<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">编号</label>
        <el-input v-model="query.stkId" clearable placeholder="编号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">产品编号</label>
        <el-input v-model="query.stkProdId" clearable placeholder="产品编号" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">仓库地址</label>
        <el-input v-model="query.stkWarehouse" clearable placeholder="仓库地址" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="产品编号" prop="stkProdId">
            <el-input v-model="form.stkProdId" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="仓库地址" prop="stkWarehouse">
            <el-input v-model="form.stkWarehouse" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="货位" prop="stkWare">
            <el-input v-model="form.stkWare" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="库存数量" prop="stkCount">
            <el-input v-model="form.stkCount" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model="form.stkMemo" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="stkId" label="编号" />
        <!--       <el-table-column prop="stkProdId" label="产品编号" /> -->
        <el-table-column prop="stkProdId" label="产品信息" width="400px">
          <template slot-scope="scope">
            <Dicproduct1 :cid="scope.row.stkProdId" />
          </template>
        </el-table-column>
        <el-table-column prop="stkWarehouse" label="仓库地址" />
        <el-table-column prop="stkWare" label="货位" />
        <el-table-column prop="stkCount" label="库存数量" />
        <el-table-column prop="stkMemo" label="备注" />
        <el-table-column v-if="checkPer(['admin','storage:edit','storage:del'])" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>

      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
    <div>
      <div id="chart" style="width: 38rem; height: 38rem;display:inline-block;" />
      <div id="main1" style="width: 38rem; height: 38rem;display:inline-block;" />
      <div id="main2" style="width: 30rem; height: 30rem;display:inline-block;" />
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import Dicproduct1 from './product.vue'
import crudStorage from '@/api/storage'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { stkId: null, stkProdId: null, stkWarehouse: null, stkWare: null, stkCount: null, stkMemo: null }
export default {
  name: 'Storage',
  components: { pagination, crudOperation, rrOperation, udOperation, Dicproduct1 },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '商品库存', url: 'api/storage', idField: 'stkId', sort: 'stkId,desc', crudMethod: { ...crudStorage }})
  },
  data() {
    return {
      list: [],
      permission: {
        add: ['admin', 'storage:add'],
        edit: ['admin', 'storage:edit'],
        del: ['admin', 'storage:del']
      },
      rules: {
        stkProdId: [
          { required: true, message: '产品编号不能为空', trigger: 'blur' }
        ],
        stkWarehouse: [
          { required: true, message: '仓库地址不能为空', trigger: 'blur' }
        ],
        stkWare: [
          { required: true, message: '仓库编号不能为空', trigger: 'blur' }
        ],
        stkCount: [
          { required: true, message: '库存数量不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'stkId', display_name: '编号' },
        { key: 'stkProdId', display_name: '产品编号' },
        { key: 'stkWarehouse', display_name: '仓库地址' }
      ]
    }
  },
  mounted() {
    setTimeout(() => {
      const data1 = []
      const data2 = []
      this.list.forEach(e => {
        data1.push(e.stkId)
        data2.push(e.stkCount)
      })
      const data3 = this.list.map((item, index) => {
        return Object.assign({}, { 'name': item.stkId, 'value': item.stkCount })
      })
      console.log(data1)
      this.chart = echarts.init(document.getElementById('chart'))
      const chartData = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: data1
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: data2,
            type: 'bar',
            showBackground: true,
            backgroundStyle: {
              color: 'rgba(180, 180, 180, 0.2)'
            }
          }
        ]
      }
      this.chart.setOption(chartData)
      this.main1 = echarts.init(document.getElementById('main1'))
      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'category',
          data: data1
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: data2,
            type: 'line'
          }
        ]
      }
      option && this.main1.setOption(option)

      this.main2 = echarts.init(document.getElementById('main2'))
      const option1 = {
        tooltip: {
          trigger: 'item'
        },
        legend: {
          top: '5%',
          left: 'center'
        },
        series: [
          {
            name: '客户消费占比',
            type: 'pie',
            radius: ['40%', '70%'],
            avoidLabelOverlap: false,
            label: {
              show: false,
              position: 'center'
            },
            emphasis: {
              label: {
                show: true,
                fontSize: 40,
                fontWeight: 'bold'
              }
            },
            labelLine: {
              show: false
            },
            data: data3
          }
        ]
      }
      option1 && this.main2.setOption(option1)
    }, 1000)
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    [CRUD.HOOK.afterRefresh]() {
      this.list = this.crud.data
    }
  }
}
</script>

<style scoped>

</style>
